package com.yuven.appframework.manager

import android.app.Activity
import android.app.Application
import android.app.Application.ActivityLifecycleCallbacks
import android.os.Bundle
import android.os.Process


class AppActivitysManager : ActivityLifecycleCallbacks {

    companion object {
        @Volatile
        private var mInstance: AppActivitysManager? = null;

        fun getInstance(): AppActivitysManager {
            if (mInstance == null) {
                synchronized(AppActivitysManager::class) {
                    if (mInstance == null) {
                        mInstance = AppActivitysManager()
                    }
                }
            }
            return mInstance!!
        }

        private val TAG: String = "AppActivitysManager"
    }

    var mApplication: Application? = null
    val mActivitys: ArrayList<Activity> = ArrayList<Activity>()

    fun init(application: Application) {
        mApplication = application
        mApplication?.registerActivityLifecycleCallbacks(this)
    }

    public fun pushActivity(activity: Activity) {
        synchronized(mActivitys) {
            mActivitys.add(activity)
        }
    }

    public fun popActivity(activity: Activity) {
        synchronized(mActivitys) {
            mActivitys.remove(activity)
        }
    }

    fun activityIsExists(name: String): Boolean {
        synchronized(mActivitys) {
            for (i in 0 until mActivitys.size) {
                var activity = mActivitys[i]
                if (activity?.javaClass.name == name) {
                    return true
                }
            }
        }
        return false
    }


    fun finishActivity(cls: Class<*>) {
        synchronized(mActivitys) {
            for (i in 0 until mActivitys.size) {
                var activity = mActivitys[i]
                if (activity?.javaClass == cls && !activity?.isFinishing)
                    mActivitys.remove(activity)
                activity?.finish()
            }
        }
    }

    fun finishActivityUntilThis(activity: Activity) {
        synchronized(mActivitys) {
            var tempActivitys: ArrayList<Activity> = ArrayList()
            for (i in 0 until mActivitys.size) {
                var tempA = mActivitys.get(mActivitys.size - 1 - i);
                if (tempA?.javaClass.name == activity?.javaClass.name) {
                    break
                }
                tempActivitys.add(tempA)
            }

            for (i in 0 until tempActivitys.size) {
                var act = tempActivitys[i]
                mActivitys.remove(act)
                act?.finish()
            }

        }
    }

    fun finishAllActivity() {
        synchronized(mActivitys) {
            for (i in 0 until mActivitys.size) {
                var activity = mActivitys[i]
                activity.finish()
            }
            mActivitys.clear()
        }
    }

    fun killSelfProcess(): Boolean {
        finishAllActivity()
        Process.killProcess(Process.myPid())
        return true
    }

    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
        pushActivity(activity)
    }

    override fun onActivityStarted(activity: Activity) {

    }

    override fun onActivityResumed(activity: Activity) {

    }

    override fun onActivityPaused(activity: Activity) {
    }

    override fun onActivityStopped(activity: Activity) {
    }

    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
    }

    override fun onActivityDestroyed(activity: Activity) {
        popActivity(activity)
    }

}

